//
//  StaticView.swift
//  demo01
//
//  Created by babybus on 2025/6/26.
//
import Foundation
import UIKit
 

class StaticView: UIViewController,UICollectionViewDelegate,
                      UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,
                      MyCell.MyCellButtonDelegate{
    struct Item{
        let imageName:String;
        let title:String
        let targetType:UIViewController.Type
    }
    private let items:[Item] = [
        Item(imageName:"lock.shield", title:"隐私政策",targetType:TestViewController.self),
        Item(imageName:"star", title:"个性化推荐管理",targetType:TextTest.self),
        Item(imageName:"square.and.arrow.up", title:"个人信息共享清单",targetType:TestViewController.self),
        Item(imageName:"checklist", title:"个人信息收集清单",targetType:TextTest.self),
        Item(imageName:"network", title:"第三方信息共享清单",targetType:TestViewController.self),
        Item(imageName:"shield.lefthalf.filled", title:"儿童隐私保护指引",targetType:TextTest.self)
    ];
    private var collectionView:UICollectionView!;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.navigationController?.navigationBar.backgroundColor = .white;
        self.navigationItem.title = "隐私管理";
        
        let backButton = UIBarButtonItem(image: UIImage(systemName: "chevron.left")?.withTintColor(.gray,renderingMode: .alwaysOriginal),
                                            style: .plain,
                                            target: self,
                                            action: #selector(backAction))
        navigationItem.leftBarButtonItem = backButton;
        
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .vertical;
        
        collectionView=UICollectionView(frame: view.bounds,collectionViewLayout: layout);
        collectionView?.backgroundColor = .white;
        collectionView?.dataSource = self;
        collectionView?.delegate=self;
        collectionView?.register(MyCell.self, forCellWithReuseIdentifier: "MyCell");
        collectionView?.register(MyHeaderView.self,
                                 forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
                                 withReuseIdentifier: "MyHeader")
        view.addSubview(collectionView);
    }
    
    
    @objc private func backAction() {
        navigationController?.popViewController(animated: true)
    }
    
    // MARK: - DataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell;
        
        let item = items[indexPath.item];
//        let image = UIImage(named: item.imageName)
        let image = UIImage(systemName: item.imageName);
        cell.configure(with: image, title: item.title,targetType: item.targetType);
        cell.delegateTest = self;
        return cell;
    }
    // 实现委托方法
    func didTapButton(in cell: MyCell) {
        let nextVC = cell.targetView!.init();
        navigationController?.pushViewController(nextVC, animated: true);
    }
    func didTapButtonTest(in cell: MyCell) {
        let nextVC = cell.targetView!.init();
        navigationController?.pushViewController(nextVC, animated: true);
    }
    
    // MARK: - Header 配置
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            let header = collectionView.dequeueReusableSupplementaryView(
                ofKind: kind,
                withReuseIdentifier: "MyHeader",
                for: indexPath
            ) as! MyHeaderView
//            header.title = "区头：Section \(indexPath.section)"
            return header
        }
        return UICollectionReusableView()
    }
    
    // MARK: - DelegateFlowLayout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (collectionView.bounds.width)
        let imageHeight = 16.0;
        let textHeight:CGFloat = 40;
        //高度为宽度的 1.5 倍
        return CGSize(width: width, height: imageHeight + textHeight + 24);
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        //设置 Header 的固定高度（50pt）和宽度（填充整个 collectionView）
        return CGSize(width: collectionView.bounds.width, height: 0)
    }
    
    @objc func clickTableBtn() {
        let vc = TestViewController()
        // 设置导航跳转至TestNavController页面
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
